package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import com.tanhua.server.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class  UserController {
    @Value("${tanhua.secret}")
    private String secret;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseEntity sendVerifyCode(@RequestBody Map map) {
        String mobile = (String) map.get("phone");
        return userService.sendVerifyCode(mobile);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String mobile = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        return userService.loginVerification(mobile, code);
    }

    /**
     * 手机号登录-完善用户信息
     * POST/user/loginReginfo
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){

        userInfo.setId(UserHolder.getUser().getId());

        return userService.saveUserInfo(userInfo);
    }

    /**
     * 手机号登录-完善用户信息-上传头像
     * POST /user/loginReginfo/head
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity saveAvatar(MultipartFile headPhoto) throws IOException {
        return userService.saveAvatar(headPhoto, UserHolder.getUser().getId());
    }
}
